﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace UIT_CV_Management
{
    public class DaoTaoTrenDaiHoc
    {
        public String ID;
        public String maDaoTaoTrenDaiHoc;
        public String maThucTap;
        public String maNghienCuu;

        //object
        public ThucTap thucTap;
        public NghienCuu nghienCuu;
        //database
        Database myDatabase = Database.shareDatabase;
        public NghienCuu getNghienCuu()
        {
            return new NghienCuu(maNghienCuu);
        }
        public ThucTap getThucTap()
        {
            return new ThucTap(maThucTap);
        }
        public DaoTaoTrenDaiHoc(ThucTap _thucTap, NghienCuu _nghienCuu)
        {
            //ID
            ID = Database.getID("DaoTaoTrenDaiHoc", "ID");
            maDaoTaoTrenDaiHoc = Database.getID("DaoTaoTrenDaiHoc", "MaDaoTaoTrenDaiHoc");

            // Object
            if (_thucTap != null)
            {
                thucTap = _thucTap;
                maThucTap = _thucTap.maThucTap;
            }
            else
                maThucTap = "";

            if (_nghienCuu != null)
            {
                nghienCuu = _nghienCuu;
                maNghienCuu = _nghienCuu.maNghienCuu;
            }
            else
                maNghienCuu = "";


        }
        public DaoTaoTrenDaiHoc(String _id)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM DaoTaoTrenDaiHoc WHERE ID = '" + _id + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                //ID
                ID = table.Rows[0]["ID"].ToString();
                maDaoTaoTrenDaiHoc = table.Rows[0]["MaTaoDaoTrenDaiHoc"].ToString();
                maThucTap = table.Rows[0]["MaThucTap"].ToString();
                maNghienCuu = table.Rows[0]["MaNghienCuu"].ToString();

            }
        }
        public Boolean Save()
        {
            if (thucTap != null)
            {
                String sqlTotNghiep = "INSERT INTO DaoTaoTrenDaiHoc VALUES('"
                    + ID + "','"
                    + maDaoTaoTrenDaiHoc + "','"
                    + maThucTap + "',"
                    + "NULL)";
                QueryResult result = myDatabase.Query(sqlTotNghiep);
                if (result.ReturnCode == 409)
                    return false;
                else // save successfully
                {
                    if (thucTap != null)
                        thucTap.Save();

                    if (nghienCuu != null)
                        nghienCuu.Save();
                    return true;
                }
            }
            else if (nghienCuu != null)
            {
                String sqlTotNghiep = "INSERT INTO DaoTaoTrenDaiHoc VALUES('"
                    + ID + "','"
                    + maDaoTaoTrenDaiHoc + "',"
                    + "NULL,'"
                    + maNghienCuu + "')";
                QueryResult result = myDatabase.Query(sqlTotNghiep);
                if (result.ReturnCode == 409)
                    return false;
                else // save successfully
                {
                    if (thucTap != null)
                        thucTap.Save();

                    if (nghienCuu != null)
                        nghienCuu.Save();
                    return true;
                }
            }
            else if (nghienCuu != null && thucTap != null)
            {
                String sqlTotNghiep = "INSERT INTO DaoTaoTrenDaiHoc VALUES('"
                    + ID + "','"
                    + maDaoTaoTrenDaiHoc + "','"
                    + maThucTap + "','"
                    + maNghienCuu + "')";
                QueryResult result = myDatabase.Query(sqlTotNghiep);
                if (result.ReturnCode == 409)
                    return false;
                else // save successfully
                {
                    if (thucTap != null)
                        thucTap.Save();

                    if (nghienCuu != null)
                        nghienCuu.Save();
                    return true;
                }
            }

            

            
            return true;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM DaoTaoTrenDaiHoc WHERE ID = '" + ID + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                thucTap.Delete();
                nghienCuu.Delete();
                return true;
            }
        }
    }
}
